package 剑指专题.其他;


/*
 * Author：江松
 * Date：2023/4/3 20:13
 *
 把数组排成最小的数:
 1,贪心，排序：按照每位数字大小排序，小的放前面。长度不同的可以，用%方式循环取
 2，处理成字符串然后比较
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Main7 {
    public String PrintMinNumber(int [] numbers) {
        String []arr=new String[numbers.length];
        for(int i=0;i<numbers.length;++i){
            arr[i]=numbers[i]+"";
        }
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return (o1+o2).compareTo(o2+o1);
            }
        });
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<arr.length;++i){
            sb.append(arr[i]);
        }
        return sb.toString();
    }
    /*
        public String PrintMinNumber(int [] numbers) {
            if(numbers==null||numbers.length==0)return "";
            Integer nums[]=Arrays.stream(numbers).boxed().toArray(Integer[]::new);
            Arrays.sort(nums, new Comparator<Integer>() {
                public ArrayList<Integer> getBits(Integer n){
                    ArrayList<Integer>arr=new ArrayList<>();
                    //个位到高位,高位放数组前边
                    while(n>0){
                        arr.add(0,n%10);
                        n/=10;
                    }
                    return arr;
                }
                @Override
                public int compare(Integer o1, Integer o2) {
                    ArrayList<Integer>arr1=getBits(o1);
                    ArrayList<Integer>arr2=getBits(o2);
                    int len1=arr1.size(),len2=arr2.size(),mlen=Math.max(len1,len2);
                    int i=0;
                    for(i=0;i<mlen;++i){
                        int a1=arr1.get(i%len1),a2=arr2.get(i%len2);
                        if(a1==a2)continue;
                        else if(a1<a2){
                            return -1;
                        }else{
                            return 1;
                        }
                    }
                    return 0;
                }
            });
            StringBuilder sb=new StringBuilder();
            for(int i:nums){
                sb.append(i);
            }
            return sb.toString();
        }
        */
}
